package com.af.dao.pay.impl;

import java.util.Map;
import java.util.TreeMap;

import org.apache.log4j.Logger;

import com.af.common.CommonUtil;
import com.af.common.GetSequence;
import com.af.common.GlobalVariables;
import com.af.common.Log4jXmlConfig;
import com.af.common.PropertiesManager;
import com.af.common.SignUtil;
import com.af.dao.pay.IPayToApi;
import com.af.dao.pay.PayEntity;
import com.af.red.AddUserGolds;
import com.af.red.GoldItem;

public class PayToApi implements IPayToApi {

	Logger LOG = Log4jXmlConfig.getInstance().returnLogger(PayToApi.class);
	private ValPayState vps = null;
	@Override
	public void YSQCX(PayEntity order) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new TreeMap<String, Object>();

		map.put("service_id", GlobalVariables.YSQCX);

		// 发起方机构编码
		map.put("req_org_cd", order.getSeller_id());

		// 发起时间
		map.put("req_trans_tm", CommonUtil.returnDateFormat("yyyyMMddHHmmss"));

		// 订单号
		map.put("req_trans_seq", GetSequence.returnSeqNum());

		map.put("order_id", order.getOrderid());

		map.put("seller_id", order.getSeller_id());

		map.put("sub_order_flag", order.getSub_order_flag());

		map.put("sub_order_num", order.getSub_order_num() + "");

		map.put("order_desc", CommonUtil
				.isNotNullOrEmpty(order.getOrder_desc()) ? order
				.getOrder_desc() : "");

		map.put("tot_trans_amt", order.getTot_trans_amt());

		map.put("buyer_id", order.getBuyer_id());

		map.put("valid_date", order.getValid_date() + "");

		map.put("notify_url", order.getNotify_url());

		map.put("sign", SignUtil.sign(map, GlobalVariables.PAY_KEY));

		map.put("sign_type", "MD5");
		
		vps = new ValPayState();
		
		if(vps.valPayResponse(order.getOrderid(), GlobalVariables.YSQCX)==true){
			
			new WebSocket().returnWebResponse(map);
		
		}
		
	}

	@Override
	public void YSQWC(PayEntity order) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new TreeMap<String, Object>();

		map.put("service_id", GlobalVariables.YSQWC);

		// 发起方机构编码
		map.put("req_org_cd", order.getSeller_id());

		// 发起时间
		map.put("req_trans_tm", CommonUtil.returnDateFormat("yyyyMMddHHmmss"));

		// 订单号
		map.put("req_trans_seq", GetSequence.returnSeqNum());

		map.put("order_id", order.getOrderid());

		map.put("seller_id", order.getSeller_id());

		map.put("sub_order_flag", order.getSub_order_flag());

		map.put("sub_order_num", order.getSub_order_num() + "");

		map.put("order_desc", CommonUtil
				.isNotNullOrEmpty(order.getOrder_desc()) ? order
				.getOrder_desc() : "");

		map.put("tot_trans_amt", order.getTot_trans_amt());

		map.put("buyer_id", order.getBuyer_id());

		map.put("valid_date", order.getValid_date() + "");

		map.put("notify_url", order.getNotify_url());

		map.put("sign", SignUtil.sign(map, GlobalVariables.PAY_KEY));

		map.put("sign_type", "MD5");
		
		vps = new ValPayState();
		
		if(vps.valPayResponse(order.getOrderid(), GlobalVariables.YSQWC)==true){
			
			double fee=0;
			int golds=0;
			// 话费卖家 100:2
			if(order.getSeller_id().equals(GlobalVariables.MOBILE_SELLER_ID)){
				fee = Double.parseDouble(order.getMobileRealPrice().toString());
			}
			// 机票卖家 100:5
			if(order.getSeller_id().equals(GlobalVariables.FLIGHT_SELLER_ID)){
				fee = Double.parseDouble(order.getTot_trans_amt().toString())/100;
			}
			golds=(int)(fee/50);
			
			
			if(golds>0){
				
				GoldItem gi = new GoldItem();
				
				gi.setGold(golds*10);
				
				gi.setUserName(order.getBuyer_id());
				
				gi.setOid(order.getOrderid());
				
				String strSeller=null;
				
				int codeType=0;
				
				if(order.getSeller_id().equals(GlobalVariables.FLIGHT_SELLER_ID)){
					
					strSeller="积分奖励【机票预订】";
					
					codeType=6;
					
				}
				
				if(order.getSeller_id().equals(GlobalVariables.MOBILE_SELLER_ID)){
					
					strSeller="积分奖励【话费充值】";
					
					codeType=5;
					
				}
				
				gi.setFromType(codeType);
				
				gi.setFromSys(3);
				
				gi.setRemarks(strSeller);
				
				new AddUserGolds().addGoldsToMember(gi);
				
				
			}
			new WebSocket().returnWebResponse(map);
			
		}

	}

	/**
	 * 退货，机票预订退票时调用
	 */
	@Override
	public void REFUND(PayEntity order) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new TreeMap<String, Object>();

		map.put("service_id", GlobalVariables.REFUND);

		// 发起方机构编码
		map.put("req_org_cd", order.getSeller_id());

		// 发起时间
		map.put("req_trans_tm", CommonUtil.returnDateFormat("yyyyMMddHHmmss"));

		// 订单号
		map.put("req_trans_seq", GetSequence.returnSeqNum());

		map.put("order_id", order.getOrderid());

		map.put("seller_id", order.getSeller_id());

		map.put("sub_order_flag", order.getSub_order_flag());

		map.put("sub_order_num", order.getSub_order_num()+"");

		map.put("order_desc", CommonUtil
				.isNotNullOrEmpty(order.getOrder_desc()) ? order
				.getOrder_desc() : "");

		map.put("tot_trans_amt", order.getTot_trans_amt().toString());

		map.put("buyer_id", order.getBuyer_id());

		map.put("valid_date", order.getValid_date() + "");

		map.put("notify_url", order.getNotify_url());

		map.put("sign", SignUtil.sign(map, GlobalVariables.PAY_KEY));

		map.put("sign_type", "MD5");
		
		new WebSocket().returnWebResponse(map);

	}
	
	public static void main(String[] args) {
		PayEntity pe = new PayEntity();
		pe.setOrderid("WM110826163113391");
		pe.setSeller_id("19pay");
		pe.setBuyer_id("00006");
		pe.setSub_order_flag("N");
		pe.setSub_order_num(0);
		pe.setOrder_desc("话费退款记录：0.01");
		pe.setTot_trans_amt("1");
		pe.setNotify_url(PropertiesManager.getInstance().returnValue(
		"PAY_FLIGHT_NOTICE"));
		new PayToApi().REFUND(pe);
	}
}
